import { Controller } from 'egg';
import { Result } from '../util/result';
import { pageSizeCommonRule, uuidCommonRule } from './validate-rule';
import MsgEnum from '../util/msgEnum';
import xlsx from 'node-xlsx';

const toArray = require('stream-to-array');

export default class ChartController extends Controller {
  /**
   * 查询企业库首页企业列表
   */
  public async showList() {
    const { ctx } = this;
    ctx.validate({
      page: { type: 'number' },
      pageSize: pageSizeCommonRule,
      name: {
        required: false,
        type: 'string',
      },
      medicalField: {
        required: false,
        ...uuidCommonRule,
      },
    }, ctx.query);

    const { page, pageSize, name, medicalField } = ctx.query;
    const { companyList, total } = await ctx.service.company.showList(
      Number(page), Number(pageSize), name, medicalField
    );
    ctx.body = new Result({ companyList, total });
  }

  /**
   * 单项添加
   */
  public async insert() {
    const { ctx } = this;
    ctx.validate({
      address: { type: 'string', required: false },
      addressBefore: { type: 'array' },
      companyNum: { type: 'string', required: false },
      createDate: { type: 'string', required: false },
      description: { type: 'string' },
      filler: { type: 'string' },
      name: { type: 'string' },
      mainBusiness: { type: 'array' },
      medicalField: { type: 'string' },
      type: { type: 'string' },
    }, ctx.request.body);
    const {
      address,
      addressBefore,
      companyNum,
      createDate,
      description,
      filler,
      name,
      mainBusiness,
      medicalField,
      type
    } = ctx.request.body;
    const result = await ctx.service.company.insert(address,
      addressBefore,
      companyNum,
      createDate,
      description,
      filler,
      name,
      mainBusiness,
      medicalField,
      type);
    ctx.body = new Result(result);
  }

  /**
   * 修改
   */
  public async update() {
    const { ctx } = this;
    ctx.validate({
      uuid: { type: 'string' },
      address: { type: 'string', required: false },
      addressBefore: { type: 'array' },
      companyNum: { type: 'string', required: false },
      createDate: { type: 'string', required: false },
      description: { type: 'string' },
      filler: { type: 'string' },
      name: { type: 'string' },
      mainBusiness: { type: 'array' },
      medicalField: { type: 'string' },
      type: { type: 'string' },
    }, ctx.request.body);
    const {
      uuid,
      address,
      addressBefore,
      companyNum,
      createDate,
      description,
      filler,
      name,
      mainBusiness,
      medicalField,
      type
    } = ctx.request.body;
    const result = await ctx.service.company.update(
      uuid,
      address,
      addressBefore,
      companyNum,
      createDate,
      description,
      filler,
      name,
      mainBusiness,
      medicalField,
      type);
    ctx.body = new Result(result);
  }

  /**
   * 删除企业
   */
  public async delete() {
    const { ctx } = this;
    ctx.validate({
      id: {
        type: 'string',
      },
    }, ctx.request.body);
    const { id } = ctx.request.body;
    const result = await ctx.service.company.delete(
      id
    );
    ctx.body = new Result(result);
  }

  /**
   * 批量删除企业
   */
  public async bulkDelete() {
    const { ctx } = this;
    ctx.validate({
      batchDeleteArray: {
        type: 'array',
      },
    }, ctx.request.body);
    const { batchDeleteArray } = ctx.request.body;
    const result = await ctx.service.company.bulkDelete(
      batchDeleteArray
    );
    ctx.body = new Result(result);
  }


  public async getCompanyMap() {
    const { ctx } = this;
    const data = await this.ctx.service.company.getCompanyMap();
    if (data) {
      ctx.body = new Result(data, MsgEnum.GET_FINANCIAL_LIST_SUCCESS);

    } else {
      ctx.status = 400;
      ctx.body = new Result(data, MsgEnum.GET_FINANCIAL_LIST_SUCCESS);

    }
  }

  async getCompanyMedication() {
    const { ctx } = this;
    const data = await this.ctx.service.company.getCompanyMedication();
    if (data) {
      ctx.body = new Result(data, MsgEnum.GET_FINANCIAL_LIST_SUCCESS);

    } else {
      ctx.status = 400;
      ctx.body = new Result(data, MsgEnum.GET_FINANCIAL_LIST_SUCCESS);

    }
  }

  public async getAllCompanyType() {
    const { ctx } = this;
    const data = await this.ctx.service.company.getAllCompanyType();
    if (data) {
      ctx.body = new Result(data, MsgEnum.GET_FINANCIAL_LIST_SUCCESS);

    } else {
      ctx.status = 400;
      ctx.body = new Result(data, MsgEnum.GET_FINANCIAL_LIST_SUCCESS);

    }
  }

  /**
   * 批量添加企业
   */
  public async bulkIncrease() {
    const { ctx } = this;
    ctx.validate({
      batchIncreaseArray: {
        type: 'array',
      },
    }, ctx.request.body);
    const { batchIncreaseArray } = ctx.request.body;
    const result = await ctx.service.company.bulkIncrease(
      batchIncreaseArray
    );
    ctx.body = new Result(result);
  }

  /**
   * 查询企业库的详细信息
   */
  public async companyDetail() {
    const { ctx } = this;
    ctx.validate({
      uuid: { type: 'string' },
    }, ctx.query);
    console.log(ctx.query);
    const { uuid } = ctx.query;
    const companyDe = await ctx.service.company.getCompanyDetail(uuid);
    ctx.body = new Result(
      companyDe
    );
  }

  async uploadFiles() {

    const { ctx } = this;
    const stream = await ctx.getFileStream();
    const newBuffer = await toArray(stream)
      .then(function (parts) {
        const buffers = [];
        for (let i = 0, l = parts.length; i < l; ++i) {
          const part = parts[i];
          buffers.push((part instanceof Buffer) ? part as never : new Buffer(part) as never);
        }
        return Buffer.concat(buffers);
      });
    const parse = xlsx.parse(newBuffer, { bookType: 'xlsx', bookSST: false, type: 'buffer' });
    const data = parse[0].data.slice(3);
    const res = await this.ctx.service.company.insertCompanies(
      data,
      stream.fields.filler
    );

    if (res.mainData) {
      ctx.body = res;
    } else {
      ctx.status = 406;
      ctx.body = res;
    }
  }
}
